import requests
import json
import execjs

cookies = {
    'abRequestId': '20a5f287-8aca-5bab-9fdf-45cfb94cff09',
    'a1': '18aa656343584vnxom74uo55f3zeucvkcemzlytdy30000269770',
    'webId': '5764d58efe63dd43d91b4b0795425628',
    'gid': 'yY00K2KqjjSjyY00K2Kq4dv9q2Y4hICl6W476fvl22iquuq8d7ShT1888JKjWW88iYSifdfS',
    'web_session': '030037a23490533fc2d07f843d224a488e882c',
    'xsecappid': 'xhs-pc-web',
    'webBuild': '4.16.0',
    'acw_tc': '7ff0e5267e7b60c9192b16c130e0624f0901d8236b147dc48e0853a47882a3cf',
    'websectiga': '16f444b9ff5e3d7e258b5f7674489196303a0b160e16647c6c2b4dcb609f4134',
    'sec_poison_id': 'b560ffe1-62bc-48ed-98d7-0b1926f1f90d',
    'unread': '{%22ub%22:%2266442062000000001e020fd3%22%2C%22ue%22:%2266246f60000000001c00959c%22%2C%22uc%22:25}',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'no-cache',
    'content-type': 'application/json;charset=UTF-8',
    # 'cookie': 'abRequestId=20a5f287-8aca-5bab-9fdf-45cfb94cff09; a1=18aa656343584vnxom74uo55f3zeucvkcemzlytdy30000269770; webId=5764d58efe63dd43d91b4b0795425628; gid=yY00K2KqjjSjyY00K2Kq4dv9q2Y4hICl6W476fvl22iquuq8d7ShT1888JKjWW88iYSifdfS; web_session=030037a23490533fc2d07f843d224a488e882c; xsecappid=xhs-pc-web; webBuild=4.16.0; acw_tc=7ff0e5267e7b60c9192b16c130e0624f0901d8236b147dc48e0853a47882a3cf; websectiga=16f444b9ff5e3d7e258b5f7674489196303a0b160e16647c6c2b4dcb609f4134; sec_poison_id=b560ffe1-62bc-48ed-98d7-0b1926f1f90d; unread={%22ub%22:%2266442062000000001e020fd3%22%2C%22ue%22:%2266246f60000000001c00959c%22%2C%22uc%22:25}',
    'origin': 'https://www.xiaohongshu.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.xiaohongshu.com/',
    'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
    'x-b3-traceid': 'f78287a40117f5b4',
    'x-s-common': '2UQAPsHCPUIjqArjwjHjNsQhPsHCH0rjNsQhPaHCH0P1+jhhHjIj2eHjwjQ+GnPW/MPjNsQhPUHCHdYiqUMIGUM78nHjNsQh+sHCH0c1P/G1PsHVHdWMH0ijP/YYG/GM+0PFPALh+o812BRT+AzMJALM80+C8gp04fT08nMCJoSF8oDAPeZIPeH9w/q7PsHVHdW9H0il+Ar9P/HMPAWhP/L7NsQh+UHCHSY8pMRS2LkCGp4D4pLAndpQyfRk/SzbyLleadkYp9zMpDYV4Mk/a/8QJf4hanS7ypSGcd4/pMbk/9St+BbH/gz0zFMF8eQnyLSk49S0Pfl1GflyJB+1/dmjP0zk/9SQ2rSk49S0zFGMGDqEybkea/8QJLLU/D4+PFRoafkwzBYVn/Q+2rExp/pOpFFl/L484MSx/fkwJpLI/nkVJbkxzfTyzbbhnfk8PDRLc/pypbLF/0QQ+LRgp/mwPSQV/Szz+bST/g4wySk3np4BJrMo/fYwpb8T/F4nyFRr/gS82DLMnS4b2SSTagSyzrQT/pzVyDET//pyprEknfMayrMgnfY8pr8Vnnk34MkrGAm8pFpC/p4QPLEo//++JLE3/L4zPFEozfY+2D8k/SzayDECafkyzF8x/Dzd+pSxJBT8pBYxnSznJrEryBMwzF8TnnkVybDUnfk+PS8i/nkyJpkLcfS+ySDUnpzyyLEo/fk+PDEk/SzmPDRongkwzrphnnM82bSg/fSwzFkx/L484MkLLfSyprDFnnMb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z98ByFTy89LA4gzVaLplwrSk+9p/Loq6qflC+rlA/d+gGaT3anSU2gbc4ozyLoqMagGA8/+n4bP3q04SpfF7qAbPyFlQz/mA+fpDq9Sl4eY0Lo4UaL+tqM4M4B8Qzg8ApB4rLrS34dPl+A+ALMm7nrSe+nLALo4r4eS8GLSiLn4Q4DEAPgZ98/8d4fpx/BRAnpmFaFSk/L8QPFMMqS8FaFS92fkQye8A+flB4gc6N7+gqgzYa/+QnnQM49IjNsQhwaHCN/LEP/DIPAD7+sIj2erIH0ilP7F=',

}

json_data = {
    'cursor_score': '',
    'num': 31,
    'refresh_type': 1,
    'note_index': 35,
    'unread_begin_note_id': '',
    'unread_end_note_id': '',
    'unread_note_count': 0,
    'category': 'homefeed.fashion_v3',
    'search_key': '',
    'need_num': 6,
    'image_formats': [
        'jpg',
        'webp',
        'avif',
    ],
    'need_filter_image': False,
}

add_headers = execjs.compile(open("./xhs.js").read()).call("get_headers","/api/sns/web/v1/homefeed",json_data)

print("headers:::",headers)

headers["x-s"] = add_headers["X-s"]
headers["x-t"] = str(add_headers["X-t"])

data = json.dumps(json_data, separators=(",", ":"))
response = requests.post('https://edith.xiaohongshu.com/api/sns/web/v1/homefeed', cookies=cookies, headers=headers,
                         data=data)
print(response.text)
